/*
Realistic Unit Modification v4.0 Dialog 
v4.0.0.1 [ALPHA]
Concept & Development by Draper & Raps | [VRC]
*/

private ["_helo","_callsign","_paytar","_mkrname","_type","_hint","_asloc","_stgtar","_mkrname2","_station","_pilot","_grp","_mkrnamepri","_pos1","_pos2","_dis","_dir","_mkrname1","_mkrname3"];

_helo = _this select 0;
_callsign = _this select 5;

// _paytar = [];
RUM_ASclick = false;

/* open map and select marker points and locations */
openMap [true,true];
for [{_n = 1},{_n <= 3},{_n = _n + 1}] do {
	_mkrname = "";
	_type = "";
	_hint = "";
	switch (_n) do {
		case 1: {
			_mkrname = format ["%1target",_helo];
			_type = "mil_destroy";
			_hint = "Select Target Area for CAS";
		};
		case 2: {
			_mkrname = format ["%1ingress",_helo];
			_type = "mil_start";
			_hint = "Select Ingress Point";
		};
		case 3: {
			_mkrname = format ["%1egress",_helo];
			_type = "mil_end";
			_hint = "Select Egress Point";
		};
		default {
			hint "shit";	
		};
	};
	_asloc = createMarkerLocal [_mkrname, [0,0,0]];
	_asloc setMarkerShapeLocal "ICON";
	_mkrname setMarkerTypeLocal _type;
	_stgtar = "";
	if (_n == 1) then {
		// _stgtar = format ["'%1' setMarkerPosLocal _pos,RUM_ASclick = [_pos] call ROEzone",_mkrname];
	_mapclick = ["rum_mapclick_id", "onMapSingleClick", { _this setMarkerPosLocal _pos;RUM_ASclick = [_pos] call ROEzone; }, _mkrname] call BIS_fnc_addStackedEventHandler;
	} else {
		// _stgtar = format ["'%1' setMarkerPosLocal _pos,RUM_ASclick = true",_mkrname];
	_mapclick = ["rum_mapclick_id", "onMapSingleClick", { _this setMarkerPosLocal _pos;RUM_ASclick = true; }, _mkrname] call BIS_fnc_addStackedEventHandler;
	};
	titleText [_hint, "PLAIN DOWN"];
	waitUntil{
		sleep 0.15;
		RUM_ASclick;
	};
	_mapclick = ["rum_mapclick_id", "onMapSingleClick"] call BIS_fnc_removeStackedEventHandler;
	if ((_n == 2) or (_n == 3)) then {
		_mkrname2 = format ["%1target",_helo];
		_mkrname setMarkerDirLocal ((getMarkerPos _mkrname2 select 0)-(getMarkerPos _mkrname select 0)) atan2 ((getMarkerPos _mkrname2 select 1)-(getMarkerPos _mkrname select 1));
	};
	RUM_ASclick = false;
};

/* close map screen after selecting mission locations */
titleFadeOut 0;
sleep 1;
openMap [false,false];

_station = format ["%1station",_helo];

/* variables set */
_pilot = driver _helo;
_grp = group _pilot;
_mkrnamepri = format ["%1target",_helo];
_pos1 = getpos player;
_pos2 = getMarkerpos _mkrnamepri;
_dis = (round ((_pos2 distance _pos1)/10))*10;
_dir = round (((_pos2 select 0)-(_pos1 select 0)) atan2 ((_pos2 select 1)-(_pos1 select 1)));
if(_dir < 0)then{_dir = _dir + 360};

/* dialog from pilot to player */
["5line",[player,_pilot,_callsign,_dir,_dis]] execVM (RUM_PATH+"AHel\radiodialogs.sqf");
sleep random(20);

/* set combat mode and behavior */
_helo setBehaviour "COMBAT";
_helo setCombatMode "RED";

/* create waypoints */
[format ["%1ingress",_helo],_grp,_helo] call RUM_AHel_MoveFromStation;
[format ["%1ingress",_helo],_grp,_helo] call RUM_AHel_MoveWP;
[format ["%1target",_helo],_grp,_helo] call RUM_AHel_SAD;
[format ["%1egress",_helo],_grp,_helo] call RUM_AHel_MoveWP;
[format ["%1station",_helo],_grp,_helo] call RUM_AHel_MoveToStation;



/* dialog from pilot to player */
_pilot sidechat format["This is %1, CAS mission completed. Returning back to stationed area...", _callsign ];

/* move unit back to station point and settle at low altitude */
_helo doMove [ (getPos _helo select 0), (getPos _helo select 1), RUM_AHEL_flyStationHeight];
_helo flyInheight RUM_AHEL_flyStationHeight;
sleep random(10);

/* wait for helo to settle */
waituntil{
	sleep 0.15;
	((getPos _helo select 2) <= (RUM_AHEL_flyStationHeight + 5));
};

/* dialog from pilot to player */
_pilot sidechat format["This is %1, we are back on station and standing by for further orders.", _callsign ];

/* remove markers */
_mkrname1 = format ["%1ingress",_helo];
_mkrname2 = format ["%1target",_helo];
_mkrname3 = format ["%1egress",_helo];
deletemarkerLocal _mkrname1;
deletemarkerLocal _mkrname2;
deletemarkerLocal _mkrname3;

/* exit script */
if(true)exitwith{};
